package com.gzj.environment.admin.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzj.environment.framework.annotation.PermissionRequired;
import com.gzj.environment.framework.annotation.ResponseResult;
import com.gzj.environment.enums.log.ParamPosition;
import com.gzj.environment.framework.result.ProfileResult;
import com.gzj.environment.model.system.criteria.UserCriteria;
import com.gzj.environment.model.system.dto.UserAssignDto;
import com.gzj.environment.model.system.dto.UserLoginDto;
import com.gzj.environment.model.system.dto.UserPasswordDto;
import com.gzj.environment.model.system.dto.UserSaveDto;
import com.gzj.environment.model.system.entity.User;
import com.gzj.environment.model.system.vo.UserVo;
import com.gzj.environment.module.log.annotation.AccessLog;
import com.gzj.environment.module.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: tsing
 * @create: 2021-01-22 20:35
 */
@RestController
@ResponseResult
@RequestMapping("/sys/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 分页条件查询
     * @param criteria 查询条件
     * @return
     */
    @GetMapping
    @PermissionRequired("user:list")
    @AccessLog(value = "条件查询用户信息")
    public Page<UserVo> list(UserCriteria criteria) {
        return userService.page(criteria);
    }

    /**
     * 新增用户信息
     * @param userSaveDto 用户信息
     */
    @PostMapping
    @PermissionRequired("user:add")
    @AccessLog(value = "新增用户信息", position = ParamPosition.BODY)
    public void add(@Valid @RequestBody UserSaveDto userSaveDto) {
        userService.add(userSaveDto);
    }

    /**
     * 根据id查询用户信息
     * @return
     */
    @GetMapping("/{id}")
    @PermissionRequired("user:list")
    @AccessLog(value = "查询id为[{1}]的用户信息")
    public User findOne(@PathVariable("id") Long id) {
        return userService.findOne(id);
    }

    /**
     * 更新用户信息
     * @param userSaveDto 用户信息
     */
    @PutMapping
    @PermissionRequired("user:edit")
    @AccessLog(value = "更新用户信息", position = ParamPosition.BODY)
    public void edit(@Valid @RequestBody UserSaveDto userSaveDto) {
        userService.edit(userSaveDto);
    }

    /**
     * 批量删除用户信息
     * @param ids 用户id的集合
     */
    @DeleteMapping
    @PermissionRequired("user:delete")
    @AccessLog(value = "批量删除用户信息")
    public void delete(@RequestParam("ids") List<Long> ids) {
        userService.delete(ids);
    }

    /**
     * 用户登录
     * @param dto 登录信息
     * @return
     */
    @PostMapping("/login")
    @AccessLog(value = "用户登录", position = ParamPosition.BODY)
    public Map<String, Object> login(@RequestBody UserLoginDto dto) {
        return userService.login(dto);
    }

    /**
     * 根据用户的标识查找对应的权限信息
     * @return
     */
    @GetMapping("/profile")
    @PermissionRequired
    @AccessLog(value = "查询用户自己的权限信息")
    public ProfileResult profile() {
        return userService.profile();
    }

    /**
     * 管理员重置用户密码，将密码重置为初始密码
     * @param userPasswordDto 操作需要的实体信息
     */
    @PutMapping("/resetPassword")
    @PermissionRequired("user:resetPassword")
    @AccessLog(value = "重置用户密码", position = ParamPosition.BODY)
    public void resetPassword(@RequestBody UserPasswordDto userPasswordDto) {
        userService.resetPassword(userPasswordDto);
    }

    /**
     * 用户修改自己的密码
     * @param userPasswordDto 操作需要的实体信息
     */
    @PutMapping("/editPassword")
    @PermissionRequired
    @AccessLog(value = "用户修改密码", position = ParamPosition.BODY)
    public void editPassword(@RequestBody UserPasswordDto userPasswordDto) {
        userService.editPassword(userPasswordDto);
    }

    /**
     * 当用户密码为初始密码时，强制修改用户密码
     * @param userPasswordDto 操作需要的实体信息
     */
    @PutMapping("/forceEditPassword")
    @PermissionRequired
    @AccessLog(value = "强制用户修改密码", position = ParamPosition.BODY)
    public void forceEditPassword(@RequestBody UserPasswordDto userPasswordDto) {
        userService.forceEditPassword(userPasswordDto);
    }

    /**
     * 设置用户角色
     * @param dto 设置的信息
     */
    @PutMapping("/assign")
    @PermissionRequired("user:assign")
    @AccessLog(value = "设置用户角色", position = ParamPosition.BODY)
    public void assignRole(@Valid @RequestBody UserAssignDto dto) {
        userService.assignRole(dto);
    }

    /**
     * 用户登出
     */
    @PostMapping("/logout")
    @PermissionRequired
    @AccessLog(value = "用户登出")
    public void logout() {
        userService.logout();
    }
}
